<?php
declare (strict_types=1);

namespace app\admin\model\base;

use app\BaseModel;
use think\model\relation\HasOne;

/**
 * 系统埋点记录表
 */
class SysSignPoint extends BaseModel
{

    /**
     * 标记点位枚举
     * @var array|string[]
     */
    public static array $pointArr = [
        'V1_Way_4_3_To_E_Home_Market' => 'V1_Way_4_3点击跳转E家集市',
    ];

    /**
     * 获取系统埋点记录表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->with(['userInfo' => function ($query) {
                $query->field('id,nickname,avatar');
            }])
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        foreach ($data['data'] as $k => &$v) {
            //补充数据
            $v['point_ch'] = self::$pointArr[$v['point']];
            //补充用户信息
            if (!$v['userInfo']) {
                $v['userInfo'] = ['id' => 0, 'nickname' => '无', 'avatar' => ''];
            }
        }
        return $data;
    }

    /**
     * 写入系统埋点记录表
     * @param int $user_id
     * @param string $point
     * @param string $mac_id
     * @param string $keyword
     * @return false|int|string
     */
    public static function saveSysSignPoint(int $user_id, string $point, string $mac_id, string $keyword): bool|int|string
    {
        if (!array_key_exists($point, self::$pointArr)) {
            return false;
        }
        $data['user_id'] = $user_id;
        $data['point'] = $point;
        $data['mac_id'] = $mac_id;
        $data['keyword'] = $keyword;
        $data['create_time'] = time();
        //写入数据库
        return self::insertGetId($data);
    }

    /**
     * 关联用户表
     * @return HasOne
     */
    public function userInfo(): HasOne
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

}




